package com.alibaba.citrus.cr.platform.enhance.facade.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.common.exception.FacadeException;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.platform.enhance.repository.InterfaceDataSyncConfigRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crplatformenhance.interfacedataconfig.model.InterfaceDataConfig;
import com.epoch.app.crplatformenhance.interfacedatasyncconfigquery.service.InterfaceDataSyncConfigQueryService;
import com.epoch.app.crplatformenhance.model.dto.DataCheckRequest;
import com.epoch.app.crplatformenhance.model.enums.ExecuteType;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: qianlei
 * @create: 2022/01/05 20:05
 */
@Component
@Primary
public class InterfaceDataSyncConfigQueryServiceImpl implements InterfaceDataSyncConfigQueryService {
    @Resource
    InterfaceDataSyncConfigRepository interfaceDataSyncConfigRepository;
    public static final Log log = Log.getLogger(InterfaceDataSyncConfigQueryServiceImpl.class);
    @Override
    public Result<Boolean> dataCheck(DataCheckRequest dataCheckRequest){
        //true 通过 ，false 不通过，需拦截过滤
        log.info("dataCheck " + JSON.toJSONString(dataCheckRequest));
        boolean checkResult = true;
        List<InterfaceDataConfig> interfaceDataConfigs = interfaceDataSyncConfigRepository.queryInterfaceDataConfigList(dataCheckRequest);
        log.info("interfaceDataConfigs " + JSON.toJSONString(interfaceDataConfigs));

        if (CollectionUtils.isEmpty(interfaceDataConfigs)){
            return Result.success(true);
        }
        for (InterfaceDataConfig interfaceData : interfaceDataConfigs) {
            //通配符* ，拦截所有，false校验不通过
            if (interfaceData.getExecuteType().equals(ExecuteType.ALL.getCode())){
                checkResult = false;
                break;
            }
            if (interfaceData.getExecuteType().equals(ExecuteType.DATE_SCOPE.getCode())){
                List<String> dateScope = JSONArray.parseArray(interfaceData.getExecuteValue(),String.class);
                Date startTime = null;
                Date endTime = null;
                Date variable = null;

                try {
                    if (StringUtils.isNotBlank(dataCheckRequest.getVariable())){
                        variable = DateUtils.string2Date(dataCheckRequest.getVariable(),"yyyy-MM-dd HH:mm:ss");
                    }
                    if (StringUtils.isNotBlank(dateScope.get(0)) && !ExecuteType.ALL.getCode().equals(dateScope.get(0))){
                        startTime = DateUtils.string2Date(dateScope.get(0));
                    }
                    if (StringUtils.isNotBlank(dateScope.get(1))){
                        endTime = DateUtils.string2Date(dateScope.get(1));
                    }
                } catch (Exception e) {
                    log.info("配置化类型转换异常",e);
                    throw new FacadeException("OTS-06-005-00-15-013");
                }


                //校验的截止时间和变量不能为空，否则不进行校验
                if (variable == null && endTime == null){
                    log.info("校验变量为空或者结束时间为空，不进行校验");
                    checkResult = true;
                    break;
                }
                //左闭右开原则
                if (startTime != null  && variable.before(endTime) && (variable.after(startTime) || variable.equals(startTime))){
                    checkResult = false;
                    break;
                }else {
                    if (variable.before(endTime)){
                        checkResult = false;
                        break;
                    }
                }

            }
            //如果是数值
            if (interfaceData.getExecuteType().equals(ExecuteType.NUM_VALUE.getCode())){

            }
        }
        return Result.success(checkResult);
    }
}
